<%-- 
    Document   : exam-manage-action
    Created on : Feb 14, 2012, 9:13:53 PM
    Author     : PhucAn
--%>

<%@page import="dto.User"%>
<%@page import="java.util.Date"%>
<%@page import="util.SharingUtil"%>
<%@page import="util.DateTime"%>
<%@page import="util.PagingUtil"%>
<%@page import="dto.Exam"%>
<%@page import="java.util.ArrayList"%>
<%@page import="dao.ExamDAO"%>
<%
//place your code here
//button Add exam click
    ExamDAO daoExam = new ExamDAO();
    if (request.getParameter("add") != null) {
        Exam e = new Exam();
        e.setName(request.getParameter("name"));
        e.setTime(Integer.parseInt(request.getParameter("time")));
        e.setStartTime(DateTime.parseToDate(request.getParameter("start-time"), DateTime.DATE));
        e.setEndTime(DateTime.parseToDate(request.getParameter("end-time"), DateTime.DATE));
        //e.getCls().setId(Integer.parseInt(session.getAttribute(SharingUtil.VAR_S_CLASSID).toString()));
        e.getCls().setId(SharingUtil.getCurrentClass(session).getId());
        e.setState(request.getParameter("is-public") != null);
        e.getAuthor().setId(SharingUtil.getUser(session).getId());
        e.setModifiedDate(new Date());
        if (daoExam.create(e) > 0) {
            SharingUtil.showMessage(session, "New exam is created");
        } else {
            SharingUtil.showMessage(session, "Error. Action failed");
        }
    }
    //load data
    int currentPage = PagingUtil.getPageNumber(request.getParameter("page"));
    int totalRecords = 0;
    int pageSize = PagingUtil.CONST_PAGE_SIZE;
    String pagingNav = "";
    ArrayList<Exam> list = new ArrayList<Exam>();
    if (SharingUtil.getUser(session).getRole() == User.ADMINISTRATOR) {
      list=  daoExam.getExams(currentPage, pageSize);
    } else {
        list=daoExam.getExamsOfUser(currentPage, pageSize, SharingUtil.getUser(session).getId());
    }
    totalRecords = daoExam.getTotalRecords();
    pagingNav = PagingUtil.getPageNavigator(totalRecords, pageSize, currentPage, "?pcontent=manage-exam&page=");
    request.setAttribute("list", list);
    request.setAttribute("pagingNav", pagingNav);

%>
